package com.bookmarket.utils;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;

/**
 * 文件上传工具类
 */
@Component
public class FileUtils {

    /***
     * 保存文件到指定路径
     */
    public static boolean saveFile(MultipartFile file,HttpServletRequest request) {
        // 判断文件是否为空
        if (!file.isEmpty()) {
            try {
                // 文件保存路径
                String filePath = request.getSession().getServletContext().getRealPath("/") + "/before/upload/"
                        + file.getOriginalFilename();
                // 转存文件
                file.transferTo(new File(filePath));
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 保存首页的JSON数据
     */
    public static boolean createJSONFile(String jsonString,HttpServletRequest request){
        try{
            String filePath = request.getSession().getServletContext().getRealPath("/")+"/before/template/index.json";
            File file = new File(filePath);
            if(file.exists()){//如果文件存在
                file.delete();//删除
            }
            file.createNewFile();
            Writer out = new OutputStreamWriter(new FileOutputStream(file),"utf-8");
            out.write(jsonString);//将json字符串写入
            out.flush();//清空
            out.close();//关闭

            return true;
        }catch(Exception e){
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 读取某文件夹下的所有文件
     */
    public static List<Map> readfile(HttpServletRequest request) {
        List<Map> list = new ArrayList<Map>();
        try {
            String filepath = request.getSession().getServletContext().getRealPath("/")+"/uploads";
            File file = new File(filepath);
            if (file.isDirectory()) {
                System.out.println("文件夹");
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    File readfile = new File(filepath + "\\" + filelist[i]);
                    if (!readfile.isDirectory()) {
                        Map map = new HashMap();
                        map.put("name",readfile.getName());
                        map.put("time",new Date(readfile.lastModified()));
                        list.add(map);
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("读取文件时发生 Exception:" + e.getMessage());
        }
        return list;
    }

    public static void getCreateTime(){
        String filePath = "C:\\test.txt";
        String strTime = null;
        try {
            Process p = Runtime.getRuntime().exec("cmd /C dir "
                    + filePath
                    + "/tc" );
            InputStream is = p.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line;
            while((line = br.readLine()) != null){
                if(line.endsWith(".txt")){
                    strTime = line.substring(0,17);
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("创建时间    " + strTime);
        //输出：创建时间   2009-08-17  10:21
    }
}
